package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.CommentQuery;
import cn.wolfcode.trip.base.query.StrategyCommentQuery;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper commentMapper;

    @Autowired
    private TagMapper tagMapper;

    @Override
    public PageInfo<StrategyComment> query(StrategyCommentQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> comments = commentMapper.selectForList(qo);
        return new PageInfo<StrategyComment>(comments);
    }

    @Override
    public void save(StrategyComment comment, String []tags) {
        comment.setUser(UserContext.getCurrentUser());
        commentMapper.insert(comment);

            for (int i = 0; i<tags.length; i++) {
                Tag tag = new Tag();
                tag.setName(tags[i]);
                tagMapper.insert(tag);
                commentMapper.insertRelation(comment.getId(),tag.getId());;
            }

    }
    /**lituan
     * 查出所有状态为热门的点评
     * @param state
     * @return
     */
    @Override
    public List<StrategyComment> selectHotComment(Long state) {
        List<StrategyComment> commentList =commentMapper.selectHotComment(state);
        return commentList;
    }

    /**
     * lituan
     * 根据点评id 查出该点评
     * @param id
     * @return
     */
    @Override
    public StrategyComment getCommentById(Long id) {
        return commentMapper.selectByPrimaryKey(id);
    }

    public void update(StrategyComment strategyComment) {
        commentMapper.update(strategyComment);
    }

    @Override
    public List<StrategyComment> queryComment(CommentQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> list=commentMapper.selectComment(qo);
        return list;
    }
}
